package org.xxx.epms.models.epms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.xxx.epms.common.api.CommonPage;
import org.xxx.epms.common.api.CommonResult;
import org.xxx.epms.models.epms.dto.AqiFeedbackDetailDTO;
import org.xxx.epms.models.epms.dto.AqiFeedbackParam;
import org.xxx.epms.models.epms.model.AqiFeedback;
import org.xxx.epms.models.epms.service.AqiFeedbackService;
import org.xxx.epms.models.epms.service.MonitorService;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lb
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/epms/aqiFeedback")
public class AqiFeedbackController {

    @Autowired
    private AqiFeedbackService aqiFeedbackService;
    @Autowired
    private MonitorService monitorService;

    //获取反馈列表总页数
    @RequestMapping(value = "/getFeedbackListTotalPage",method = RequestMethod.POST )
    @ApiOperation(value = "获取反馈列表总页数", notes = "获取反馈列表总页数")
    public CommonResult<Integer> getFeedbackListTotalPage(@RequestBody AqiFeedbackParam aqiFeedbackParam,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
      int totalPage = aqiFeedbackService.getTotalPage(aqiFeedbackParam, pageSize);
      System.out.println(totalPage);
      return CommonResult.success(totalPage);
    };

    //分页获取反馈列表
    @RequestMapping(value = "/getFeedbackList",method = RequestMethod.POST )
    @ApiOperation(value = "分页获取反馈列表", notes = "分页获取反馈列表")
    public CommonResult<CommonPage<AqiFeedback>> getFeedbackList(@RequestBody AqiFeedbackParam aqiFeedbackParam,
                                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<AqiFeedback> page = aqiFeedbackService.getPageList(aqiFeedbackParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    };

    //分页获取带监督员的反馈列表
    @RequestMapping(value = "/getFeedbackListDetail",method = RequestMethod.POST )
    @ApiOperation(value = "分页获取带监督员的反馈列表", notes = "分页获取带监督员的反馈列表")
    public CommonResult<CommonPage<AqiFeedbackDetailDTO>> getFeedbackListDetail(@RequestBody AqiFeedbackParam aqiFeedbackParam,
                                                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<AqiFeedback> feedbackPage = aqiFeedbackService.getPageList(aqiFeedbackParam, pageSize, pageNum);
        Page<AqiFeedbackDetailDTO> page = new Page<>();
        List<AqiFeedbackDetailDTO> feedbackList = new ArrayList<>();
        //遍历反馈列表，查询监督员信息
        for (AqiFeedback aqiFeedback : feedbackPage.getRecords()) {
            AqiFeedbackDetailDTO dto = new AqiFeedbackDetailDTO();
            dto.setAqiFeedback(aqiFeedback);
            dto.setMonitor(monitorService.getMonitorByMonId(aqiFeedback.getMonId()));
            if(aqiFeedback.getFbImg()!=null){
                dto.setImageUrl(aqiFeedback.getFbImg().split(AqiFeedback.PATH_SEPARATOR));
            }
            feedbackList.add(dto);
        }
        page.setRecords(feedbackList);
        return CommonResult.success(CommonPage.restPage(page));
    };

    @RequestMapping(value = "/assignAqiFeedback",method = RequestMethod.POST )
    @ApiOperation(value = "指派反馈", notes = "指派反馈")
    public CommonResult<Integer> assignAqiFeedback(@RequestParam(value = "fbId")Integer fbId,
                                                    @RequestParam(value = "insId")Integer insId)  {
        int result = aqiFeedbackService.assignAqiFeedback(fbId, insId);
        if(result > 0){
            System.out.println("指派反馈成功"+fbId+"       "+insId);
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }

}

